package com.jdew.thkj.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.ZhaoshangBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;
import com.jdew.thkj.onitemInterface.OnItemcheckedListener;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class SharePictureAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public List<String> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private OnItemcheckedListener OnItemcheckedListener;
    private boolean onBind;
    public int pos;
    public Map<Integer, Boolean> map = new HashMap<>();

    public void setItemCheckedListener(OnItemcheckedListener OnItemcheckedListener) {
        this.OnItemcheckedListener = OnItemcheckedListener;
    }

    public SharePictureAdapter(Context context, List<String> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setDataList(List<String> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        itemView = layoutInflater.inflate(R.layout.picture_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MyViewHolder myviewHolder = (MyViewHolder) holder;
        myviewHolder.BindView(holder, position);
        myviewHolder.itemView.setTag(position);
        ((MyViewHolder) holder).select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    map.clear();
                    map.put(position, true);
                } else {
                    if (map.size() > 1) {
                        map.remove(position);
                    }
                }
                if (!onBind) {
                    notifyDataSetChanged();
                }
            }
        });
        onBind = true;
        if (map != null && map.containsKey(position)) {
            pos = position;
            ((MyViewHolder) holder).select.setChecked(true);
        } else {
            if (map.size() != 0) {
                ((MyViewHolder) holder).select.setChecked(false);
            }
        }
        if (null != OnItemcheckedListener) {
            OnItemcheckedListener.onitemcheck(pos);
        }
        onBind = false;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.select)
        CheckBox select;


        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
            Glide.with(context).load(dataList.get(position)).into(product);
        }
    }
}
